package Lee_code;

/**
 * @author blh
 * @date 2024年10月21日 10:00
 */
public class Lee_226_1021 {
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        TreeNode newRoot = new TreeNode(root.val);
        reverseTree(root, newRoot);
        return newRoot;
    }

    private void reverseTree(TreeNode root, TreeNode newNode) {
        if (root.left != null) {
            newNode.right = new TreeNode(root.left.val);
            reverseTree(root.left, newNode.right);
        }
        if (root.right != null) {
            newNode.left = new TreeNode(root.right.val);
            reverseTree(root.right, newNode.left);
        }
    }
}
